//
//  TKClassifyViewController.m
//  Ticket
//
//  Created by 小禹宝宝 on 2017/10/9.
//  Copyright © 2017年 cy55. All rights reserved.
//

#import "TKIssueViewController.h"
#import "IJTextAndPicsCell.h"
#import "TKIssueClassifyCell.h"
#import "TKIssueTitleHearderView.h"
#import "TKIssueCell.h"
#import "TKIssueDatePickerCell.h"
#import "TKBiddingAgreementCell.h"
#import "TKIssueSellingWayView.h"

#import <ActionSheetPicker-3.0/ActionSheetPicker.h>
#import "ZFDropDown.h"

#import <AlipaySDK/AlipaySDK.h>
#import "Order.h"
//#import "WXApi.h"
#import "RSADataSigner.h"

@interface TKIssueViewController ()<
ZFDropDownDelegate
>
@property (nonatomic, strong) IJTextAndPicsCell *textAndPicsCell;
@property (nonatomic, strong) TKIssueTitleHearderView *topView;
@property (nonatomic, strong) ZFDropDown * dropDown;
@property (nonatomic, strong) ZFTapGestureRecognizer * tap;
@property (nonatomic, strong) TKIssueClassifyCell *classCell;
@property (nonatomic, strong) TKIssueSellingWayView *sellingView;
@property (nonatomic, strong) TKIssueSellingWayView *sellingCell;

@property (nonatomic, strong) UIButton *sendBtn;

@property (nonatomic, copy) NSString *selectedDate;
@property (nonatomic, copy) NSString *selectedDeposit;//保证金额
@property (nonatomic, copy) NSString *upsetPrice;//起拍价格
@property (nonatomic, copy) NSString *bidIncrement;//加价幅度
@property (nonatomic, copy) NSString *price; //一口价
@property (nonatomic, copy) NSString *inventory; //库存
@property (nonatomic, copy) NSString *postage;//邮费
//@property (nonatomic, copy) NSString *classifyStr; //分类字段
@property (nonatomic, copy) NSString *category_id; //分类ID 1-演出 2-体育赛事 3-休闲展览
@property (nonatomic, copy) NSString *sell_type; //售卖类型 1-一口价 2-拍卖
@property (nonatomic, strong) NSMutableArray *picUrlArray;  //上传图片的地址

@property (nonatomic, strong) NSArray * dataSourceDropArray;
@property (nonatomic, assign) NSInteger sellingCount;
@property (nonatomic, assign) TKMarginType marginType;
@property (nonatomic, assign) BOOL isAgree;

@end
@implementation TKIssueViewController

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
-(void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"发布宝贝";
    [self ks_setupLeftBackBarButtonItemWithColor:nil];
    self.sendBtn = [self ks_setupRightButtonItemWithTitle:@"发布"
                                                    image:nil
                                         highLightedImage:nil];
    self.sendBtn.enabled = NO;
    [self.tableView registerClass:[IJTextAndPicsCell class] forCellReuseIdentifier:NSStringFromClass([IJTextAndPicsCell class])];

    [self.tableView registerNib:[UINib nibWithNibName:@"TKIssueCell" bundle:nil] forCellReuseIdentifier:@"TKIssueCell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"TKIssueClassifyCell" bundle:nil] forCellReuseIdentifier:@"TKIssueClassifyCell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"TKIssueDatePickerCell" bundle:nil] forCellReuseIdentifier:@"TKIssueDatePickerCell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"TKBiddingAgreementCell" bundle:nil] forCellReuseIdentifier:@"TKBiddingAgreementCell"];
    self.sellingCell = [[[NSBundle mainBundle] loadNibNamed:@"TKIssueSellingWayView" owner:nil options:nil] firstObject];
    [self creaTableHeaderView];
//    [self creatDropDownView];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(configDropViewFrame) name:kNotificationGetCellHeight object:nil];

}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    if (![SystemHelper checkAndShowLoginViewControllerIfNotLogin]) {
        [self ks_showToast:@"您必须登录后才能发布"];
    }
    
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
}

- (void)ks_leftBackButtonPressed:(UIButton  *)sender {
    [self.navigationController popViewControllerAnimated:NO];
}

- (void)ks_rightButtonPressed:(id)sender {
    //发布
    [self.view endEditing:YES];

    NSLog(@"发布了");
//    self.topView.issueTitleText
//    self.textAndPicsCell.issueText     self.textAndPicsCell.picList
//    self.classifyStr
//    self.selectedDeposit      self.selectedDate
//    self.upsetPrice  self.bidIncrement  self.postage
    if (self.upsetPrice.length == 0) {
        self.upsetPrice = @"0";
    }
    if (self.bidIncrement.length == 0) {
        self.bidIncrement = @"10";
    }
    if (self.postage.length == 0) {
        self.postage = @"1";
    }
    if (self.price.length == 0) {
        self.price = @"10";
    }
    if (self.inventory.length == 0) {
        self.inventory = @"1";
    }
    if (self.topView.issueTitleText.length == 0 || self.textAndPicsCell.issueText == 0 || self.textAndPicsCell.picList.count == 0 || self.category_id == 0 || self.selectedDeposit.length == 0 || self.selectedDate.length == 0 ) {
        [self ks_showToast:@"请将资料填写完整再发布"];
        return;
    }
    //有几张照片就有几个@"pictures[]"字段
    self.picUrlArray = self.textAndPicsCell.picUrlArray;
    NSString *picString = [self.picUrlArray componentsJoinedByString:@","];

    NSDictionary *params = @{@"category_id":self.category_id,@"title":self.topView.issueTitleText,@"intro":self.textAndPicsCell.issueText,@"end_time":self.selectedDate,@"min_price":self.upsetPrice,@"step_price":self.bidIncrement,@"price":self.price,@"total_num":self.inventory,@"sell_type":self.sell_type,@"deposit":self.selectedDeposit,@"get_type":self.postage,@"pictures":picString};
    @weakify(self)
    [BLApi POST:URL_Addgoods_submit parameters:params decodeClass:[BLBaseResponse class] completion:^(id responseObject, id responseDataOriginal, NSError *error) {
        @strongify(self)
        if (!error) {
            BLBaseResponse *response = (BLBaseResponse *)responseObject;
            if ([response isSuccess]) {
                //发通知 传递类型type跳转到home对应页
                NSDictionary *dict = @{@"category_id":self.category_id};
                [[NSNotificationCenter defaultCenter] postNotificationName:kNotificationTieziPublished object:nil userInfo:dict];
                [KSNavi pop];
            } else {
                [self ks_showToast:response.errmsg];
            }
            
        } else {
            DDLogInfo(@"%@",error);
        }
    }];

}


- (void)creaTableHeaderView {
    self.topView = [[[NSBundle mainBundle] loadNibNamed:@"TKIssueTitleHearderView" owner:nil options:nil] firstObject];
    self.tableView.tableHeaderView = self.topView;
    [self.topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(self.tableView);
        make.height.mas_equalTo(51);
    }];
}

- (void)creatDropDownView:(CGFloat)yPos {
    //初始化的时候就给tableView 添加一个下拉菜单，dropDown
    CGFloat width = 100;
    CGFloat height = 25;
    CGFloat xPos = (mScreenWidth - width)/2;
    self.dropDown = [[ZFDropDown alloc] initWithFrame:CGRectMake(xPos, yPos, width, height) pattern:kDropDownPatternDefault];
    [self.tableView addSubview:self.dropDown];
    
    //        [self.dropDown mas_makeConstraints:^(MASConstraintMaker *make) {
    //            make.height.mas_equalTo(height);
    //            make.leading.mas_equalTo(0);
    //            make.trailing.mas_equalTo(0);
    //            make.bottom.mas_equalTo(-height);
    //        }];
    
    self.dropDown.delegate = self;
    self.dropDown.borderStyle = kDropDownTopicBorderStyleRect;
    //    self.dropDown.cellTextFont = [UIFont systemFontOfSize:14];
//    self.tap = [[ZFTapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction)];
//    [self.tableView addGestureRecognizer:self.tap];
}

- (void)resetSendBtnStatus
{
    if (self.textAndPicsCell.issueText.length > 0 &&self.isAgree) {
        self.sendBtn.enabled = YES;
    } else {
        self.sendBtn.enabled = NO;
    }
}

#pragma mark - NotifacationMethod
- (void)configDropViewFrame {
    CGFloat yPos = [self.textAndPicsCell cellHeight] + 51 + 12.5 + 51;
    if (!self.dropDown) {
        [self creatDropDownView:yPos];
    } else {
        [self.dropDown removeFromSuperview];
        self.dropDown = nil;
        [self creatDropDownView:yPos];
    }
}

#pragma mark - Action
- (void)agreementAction:(UIButton *)sender {
    sender.selected = !sender.selected;
    self.isAgree = sender.selected;
    if (self.isAgree) {
        [self resetSendBtnStatus];
    }
    NSLog(@"%d",self.isAgree);
}

- (void)protocol {
    //push到 对应协议webView
}

- (void)classifyAciton:(UIButton *)sender {

    [self.classCell.leftBtn setTitleColor:[BLColorConfig mainColor] forState:UIControlStateSelected];
    [self.classCell.rightBtn setTitleColor:[BLColorConfig mainColor] forState:UIControlStateSelected];
    [self.classCell.midBtn setTitleColor:[BLColorConfig mainColor] forState:UIControlStateSelected];
    [sender setTintColor:[UIColor whiteColor]];
    [sender setTitleColor:[BLColorConfig defaultBlueColor] forState:UIControlStateNormal];
    switch (sender.tag) {
        case 1000:
        {
            self.category_id = @"1";
            self.classCell.leftBtn.selected = YES;
            self.classCell.midBtn.selected = NO;
            self.classCell.rightBtn.selected = NO;

        }
            break;
        case 2000:
        {
            self.category_id = @"2";
            self.classCell.midBtn.selected = YES;
            self.classCell.rightBtn.selected = NO;
            self.classCell.leftBtn.selected = NO;

        }
            break;
        case 3000:
        {
            self.category_id = @"3";
            self.classCell.rightBtn.selected = YES;
            self.classCell.leftBtn.selected = NO;
            self.classCell.midBtn.selected = NO;
        }
            break;
        default:
            break;
    }
}

- (void)sellingAciton:(UIButton *)sender {
    [sender setTitleColor:[BLColorConfig mainColor] forState:UIControlStateSelected];
    [sender setTintColor:[UIColor whiteColor]];
    [sender setTitleColor:[BLColorConfig defaultBlueColor] forState:UIControlStateNormal];
    switch (sender.tag) {
        case 4000:
        {
            //一口价

            self.sellingCount = 2;
            self.sell_type = @"1";
            [self.tableView reloadData];
            self.sellingView.leftBtn.selected = YES;
            self.sellingView.rightBtn.selected = NO;
        }
            break;
        case 5000:
        {
            //拍卖
            self.sellingCount = 2;
            self.sell_type = @"2";
            [self.tableView reloadData];
            self.sellingView.rightBtn.selected = YES;
            self.sellingView.leftBtn.selected = NO;
        }
            break;
        default:
            break;
    }
    
}

- (void)sellingCellAciton:(UIButton *)sender {
    [sender setTitleColor:[BLColorConfig mainColor] forState:UIControlStateSelected];
    [sender setTintColor:[UIColor whiteColor]];
    [sender setTitleColor:[BLColorConfig defaultBlueColor] forState:UIControlStateNormal];
    switch (sender.tag) {
        case 6000:
        {
            self.postage = @"1";
            self.sellingCell.leftBtn.selected = YES;
            self.sellingCell.rightBtn.selected = NO;
        }
            break;
        case 7000:
        {
            self.postage = @"2";
            self.sellingCell.rightBtn.selected = YES;
            self.sellingCell.leftBtn.selected = NO;
        }
            break;
        default:
            break;
    }
    
}
#pragma mark - UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return 4;
    } else if (section == 1){
        return self.sellingCount;
    } else {
        return 2;
    }
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 3;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            if (!self.textAndPicsCell) {
                self.textAndPicsCell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([IJTextAndPicsCell class])];
                @weakify(self);
                self.textAndPicsCell.reloadDataBlock = ^{
                    @strongify(self);
                    [self.tableView reloadData];
                };
                self.textAndPicsCell.textChangedBlock = ^{
                    @strongify(self);
                    [self resetSendBtnStatus];
                };
            }
            return self.textAndPicsCell;
        }
        
        if (indexPath.row == 1) {
            self.classCell = [tableView dequeueReusableCellWithIdentifier:@"TKIssueClassifyCell" forIndexPath:indexPath];
            self.classCell.leftBtn.tag = 1000;
            self.classCell.midBtn.tag = 2000;
            self.classCell.rightBtn.tag = 3000;
            [self.classCell.leftBtn addTarget:self action:@selector(classifyAciton:) forControlEvents:UIControlEventTouchUpInside];
            [self.classCell.midBtn addTarget:self action:@selector(classifyAciton:) forControlEvents:UIControlEventTouchUpInside];
            [self.classCell.rightBtn addTarget:self action:@selector(classifyAciton:) forControlEvents:UIControlEventTouchUpInside];
            return self.classCell;
        }
        
        if (indexPath.row == 2) {
            //保证金
            TKIssueCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TKIssueCell" forIndexPath:indexPath];
            cell.titleLabel.text = @"保证金";
            cell.priceTextField.hidden = YES;
            return cell;
        }
        
        if (indexPath.row == 3) {
            TKIssueDatePickerCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TKIssueDatePickerCell" forIndexPath:indexPath];
            if (self.selectedDate.length != 0) {
                cell.dateLabel.text = self.selectedDate;
            }
            return cell;
        }

    } else if (indexPath.section == 1){
        TKIssueCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TKIssueCell" forIndexPath:indexPath];
        
        if (indexPath.row == 0) {
            //起拍
            if ([self.sell_type isEqualToString:@"1"]) {
                cell.titleLabel.text = @"价格";
                cell.priceTextField.text = @"0";
                @weakify(self);
                cell.textBlock = ^(NSString *text) {
                    @strongify(self);
                    self.price = text;
                };
            } else {
                cell.titleLabel.text = @"起拍价格";
                cell.priceTextField.text = @"0";
                @weakify(self);
                cell.textBlock = ^(NSString *text) {
                    @strongify(self);
                    self.upsetPrice = text;
                };
            }

        }
        if (indexPath.row == 1) {
            //加价
            if ([self.sell_type isEqualToString:@"1"]) {
                cell.titleLabel.text = @"库存数量";
                cell.priceTextField.text = @"0";
                @weakify(self);
                cell.textBlock = ^(NSString *text) {
                    @strongify(self);
                    self.inventory = text;
                };
            } else {
                cell.titleLabel.text = @"加价幅度";
                cell.priceTextField.text = @"10";
                @weakify(self);
                cell.textBlock = ^(NSString *text) {
                    @strongify(self);
                    self.bidIncrement = text;
                };
            }
        }
        return cell;
    }  else {
        if (indexPath.row == 1) {
            TKBiddingAgreementCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TKBiddingAgreementCell" forIndexPath:indexPath];
            [cell.choseBtn addTarget:self action:@selector(agreementAction:) forControlEvents:UIControlEventTouchUpInside];
            [cell.protocolBtn addTarget:self action:@selector(protocol) forControlEvents:UIControlEventTouchUpInside];
            return cell;
        }
        if (indexPath.row == 0) {
            [self.sellingCell.leftBtn setTitle:@"包邮" forState:UIControlStateNormal];
            [self.sellingCell.rightBtn setTitle:@"现场取票" forState:UIControlStateNormal];
            self.sellingCell.leftBtn.tag = 6000;
            self.sellingCell.rightBtn.tag = 7000;
            [self.sellingCell.leftBtn addTarget:self action:@selector(sellingCellAciton:) forControlEvents:UIControlEventTouchUpInside];
            [self.sellingCell.rightBtn addTarget:self action:@selector(sellingCellAciton:) forControlEvents:UIControlEventTouchUpInside];
            return  self.sellingCell;
        }
    }
    return nil;
}

#pragma mark - UITableViewDelegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            if ([self.textAndPicsCell cellHeight] > 0) {
                [[NSNotificationCenter defaultCenter] postNotificationName:kNotificationGetCellHeight object:nil];
            }
            return [self.textAndPicsCell cellHeight];
        }
    }
    return 51;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 1) {
        return 51;
    } else {
        return 0;
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    if (self.sellingView) {
        return self.sellingView;
    } else {
        self.sellingView = [[[NSBundle mainBundle] loadNibNamed:@"TKIssueSellingWayView" owner:nil options:nil] firstObject];
        self.sellingView.leftBtn.tag = 4000;
        self.sellingView.rightBtn.tag = 5000;
        [self.sellingView.leftBtn addTarget:self action:@selector(sellingAciton:) forControlEvents:UIControlEventTouchUpInside];
        [self.sellingView.rightBtn addTarget:self action:@selector(sellingAciton:) forControlEvents:UIControlEventTouchUpInside];
        return self.sellingView;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.section == 0) {
        switch (indexPath.row) {
            case 0:
                break;
            case 1: {
                
            }
                break;
            case 3: {
                //截止时间
                @weakify(self)
                ActionSheetDatePicker *picker = [[ActionSheetDatePicker alloc] initWithTitle:@"截止时间" datePickerMode:UIDatePickerModeDateAndTime selectedDate:[NSDate date] doneBlock:^(ActionSheetDatePicker *picker, id selectedDate, id origin) {
                    @strongify(self)
//                    2017-10-01 00:00:00
                    NSDateFormatter *formatter = [NSDateFormatter jc_cachedDateFormatter:@"yyyy-MM-dd hh:mm:ss"];
                    self.selectedDate = [formatter stringFromDate:selectedDate];
                    NSLog(@"%@",self.selectedDate);
                    [self.tableView reloadData];
                } cancelBlock:^(ActionSheetDatePicker *picker) {
                    
                } origin:self.view];
                picker.minimumDate = [NSDate date];
                picker.maximumDate = [NSDate dateWithTimeIntervalSinceNow:7 * 24 * 3600];
                
                UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithTitle:@"取消" style:UIBarButtonItemStylePlain target:nil action:nil];
                [picker setCancelButton:cancelButton];
                UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithTitle:@"完成" style:UIBarButtonItemStylePlain target:nil action:nil];
                [picker setDoneButton:doneButton];
                picker.tapDismissAction = TapActionCancel;
                [picker showActionSheetPicker];
                
                
            }
                break;
            default:
                break;
        }
    }
}

#pragma mark - ZFDropDownDelegate

- (NSArray *)itemArrayInDropDown:(ZFDropDown *)dropDown{
    return self.dataSourceDropArray;
}

/**
 *  返回自定义topic(当dropDown为kDropDownPatternDefault时，该方法无效)
 */
- (UIView *)viewForTopicInDropDown:(ZFDropDown *)dropDown {

    return nil;
}

/**
 *  返回自定义cell(当dropDown为kDropDownPatternDefault时，该方法无效)
*/
- (UITableViewCell *)dropDown:(ZFDropDown *)dropDown tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    return nil;
}

- (void)dropDown:(ZFDropDown *)dropDown didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"下拉菜单点击了第%zd行",indexPath.row + 1);
    
    self.marginType = indexPath.row;
    self.selectedDeposit = self.dataSourceDropArray[self.marginType];
}

- (CGFloat)dropDown:(ZFDropDown *)dropDown heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 51;
}

- (NSUInteger)numberOfRowsToDisplayIndropDown:(ZFDropDown *)dropDown itemArrayCount:(NSUInteger)count {
    return self.dataSourceDropArray.count;
}



- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.view endEditing:YES];
}


-(void)scrollViewDidScroll:(UIScrollView *)scrollView {
    [self.dropDown resignDropDownResponder];
}
/**
 *  self.view添加手势取消dropDown第一响应
 */
- (void)tapAction{
    [self.dropDown resignDropDownResponder];
}
#pragma mark -lazyLoad
- (NSArray *)dataSourceDropArray{
    
    if (!_dataSourceDropArray) {
        _dataSourceDropArray = [NSArray arrayWithObjects:@"0",@"10",@"30",@"50",@"100",@"200",@"500",@"1000",@"2000", nil];
    }
    return _dataSourceDropArray;
}

#pragma mark -Alipay
- (void)aliPay {
    
    // 支付宝支付
    /* 在调用支付宝支付之前，需要我们将相关订单参数发送至我们的后台服务器，由后台服务器进行签名处理，    并返回客户端所有相关参数，客户端直接使用参数调起支付宝支付。
            
                 */
    
    //重要说明
    //这里只是为了方便直接向商户展示支付宝的整个支付流程；所以Demo中加签过程直接放在客户端完成；
    //真实App里，privateKey等数据严禁放在客户端，加签过程务必要放在服务端完成；
    //防止商户私密数据泄露，造成不必要的资金损失，及面临各种安全风险；
    /*============================================================================*/
    /*=======================需要填写商户app申请的===================================*/
    /*============================================================================*/
    NSString *appID = @"";
    //    NSString *privateKey = kCHLZFB_RSA_PRIVATE;
    NSString *rsa2PrivateKey = @"";
    NSString *rsaPrivateKey = @"";
    
    /*============================================================================*/
    /*============================================================================*/
    /*============================================================================*/
    
    //partner和seller获取失败,提示
    if ([appID length] == 0 ||
        ([rsa2PrivateKey length] == 0 && [rsaPrivateKey length] == 0))
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示"
                                                        message:@"缺少appId或者私钥。"
                                                       delegate:self
                                              cancelButtonTitle:@"确定"
                                              otherButtonTitles:nil];
        [alert show];
        return;
    }
    
    /*
     *生成订单信息及签名
     */
    //将商品信息赋予AlixPayOrder的成员变量
    Order* order = [Order new];
    
    // NOTE: app_id设置
    order.app_id = appID;
    
    // NOTE: 支付接口名称
    order.method = @"alipay.trade.app.pay";
    
    // NOTE: 参数编码格式
    order.charset = @"utf-8";
    
    // NOTE: 当前时间点
    NSDateFormatter* formatter = [NSDateFormatter new];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    order.timestamp = [formatter stringFromDate:[NSDate date]];
    
    // NOTE: 支付版本
    order.version = @"1.0";
    
    // NOTE: sign_type 根据商户设置的私钥来决定
    order.sign_type = (rsa2PrivateKey.length > 1)?@"RSA2":@"RSA";
    
    //	支付宝支付回调地址
    order.notify_url = @"";
    
    // NOTE: 商品数据
    order.biz_content = [BizContent new];
    order.biz_content.body = @"我是测试数据";
    order.biz_content.subject = @"1";
    order.biz_content.out_trade_no = @""; //订单ID（由商家自行制定）
    order.biz_content.timeout_express = @"30m"; //超时时间设置
    order.biz_content.total_amount = @""; //商品价格
    order.biz_content.seller_id = @""; //收款支付宝用户ID
    //将商品信息拼接成字符串
    NSString *orderInfo = [order orderInfoEncoded:NO];
    NSString *orderInfoEncoded = [order orderInfoEncoded:YES];
    NSLog(@"orderSpec = %@",orderInfo);
    
    // NOTE: 获取私钥并将商户信息签名，外部商户的加签过程请务必放在服务端，防止公私钥数据泄露；
    //       需要遵循RSA签名规范，并将签名字符串base64编码和UrlEncode
    NSString *signedString = nil;
    RSADataSigner* signer = [[RSADataSigner alloc] initWithPrivateKey:((rsa2PrivateKey.length > 1)?rsa2PrivateKey:rsaPrivateKey)];
    if ((rsa2PrivateKey.length > 1)) {
        signedString = [signer signString:orderInfo withRSA2:YES];
    } else {
        signedString = [signer signString:orderInfo withRSA2:NO];
    }
    
    // NOTE: 如果加签成功，则继续执行支付
    if (signedString != nil) {
        //应用注册scheme,在AliSDKDemo-Info.plist定义URL types
        NSString *appScheme = @"Ticket";
        // NOTE: 将签名成功字符串格式化为订单字符串,请严格按照该格式
        NSString *orderString = [NSString stringWithFormat:@"%@&sign=%@",
                                 orderInfoEncoded, signedString];
        // NOTE: 调用支付结果开始支付
        
        [[AlipaySDK defaultService] payOrder:nil fromScheme:appScheme callback:^(NSDictionary *resultDic) {
            
//            [[NSNotificationCenter defaultCenter] postNotificationName:JQT_ALIPAY_COMPLETE_NOTIF object:resultDic userInfo:nil];
            NSLog(@"reslut = %@",resultDic);
        }];
    
}
}


@end
